[EF + Oracle] Inserting Data (1/2)

Posted by JTorrecilla on Geeks with Blogs See other posts from Geeks with Blogs or by JTorrecilla
Published on Thu, 03 Mar 2011 15:24:49 GMT Indexed on 2011/03/03 15:25 UTC
Read the original article Hit count: 200

Filed under:

Prologue

Following EF series (I ,II y III) in this chapter we will see how to create DB record from EF.

Inserting Data

Like we indicated in the 2º post: “One Entity matches with a DB record, and one property match with a Table Column”.

To start, we need to create an object from one of the Entities:

   1: EMPLEADOS empleado = new EMPLEADOS();

Also like, I told previously, Exists the possibility to use the Static Function defined by VS for each Entity:

Once we have created the object, we can Access to it properties to fill like a common class:

 

   1: empleado.NOMBRE = "Javier Torrecilla";

 

After finish of fill our Entity properties, it must be needed to add the object to the appropriate ObjectSet in the ObjectContext:

   1: enti.EMPLEADOS.AddObject(empleado);

or

   1: enti.AddToEMPLEADOS(empleado);

Both methods will do the same action, create an insert statement.

Have we finished?

No.

Any Entity has a property called “EntityState”. This prop is an Enum from “EntityState”, which has the following:

  • Detached: the Entity is created, but not added to the Context.
  • Unchanged: There is no pending changes in the Entity.
  • Added: The entity is added to the ObjectSet, but it is not yet sent to the DB.
  • Deleted: The object is deleted form the ObjectSet, but not yet from the DB.
  • Modified: There is Pending Changes to confirm.

Let’s see, the several values of the property during the Creation steps:

1. While the Object is created and we are filling the props: EntityState.Detached;

2. After adding to the ObjectSet: EntityState.Added. This not indicated that the record is in the DB

3. Saving the Data:
To sabe the data in the DB, we are going to call “SaveChanges” method of the Object Context. After invoke it, the property will be EntityState.Unchanged.

     

    What does SaveChanges Method?

    This function will synchronize and send all pending changes to DB.

    It will add, modify or delete all Entities, whose EntityState property, is setted to Added, Deleted or Modified.

    After finishing, all added or modified entities will be change the State to “Unchanged”, and deleted Entities must take the “Detached” state.

    © Geeks with Blogs or respective owner